import com.dto.LeaveRequestDto;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
@SpringBootTest
@AutoConfigureMockMvc
class LeaveRequestControllerTest {
    @Autowired
    private MockMvc mockMvc;

    @Autowired
    private WebApplicationContext webApplicationContext;

    @BeforeEach
    public void setup() {
        this.mockMvc = MockMvcBuilders.webAppContextSetup(this.webApplicationContext).build();
    }

    // 测试提交请假申请
    @Test
    public void testSubmitLeaveRequest() throws Exception {
        LeaveRequestDto leaveRequestDto = new LeaveRequestDto();
        leaveRequestDto.setStudentId(1L);
        leaveRequestDto.setReason("I'm sick");

        mockMvc.perform(post("/api/leave/submit")
                        .contentType(MediaType.APPLICATION_JSON)
                        .content(asJsonString(leaveRequestDto)))
                .andExpect(status().isCreated())
                .andExpect(jsonPath("$.id").value(1)) // 这里需要调整以匹配实际返回的ID生成逻辑
                .andExpect(jsonPath("$.studentId").value(1L))
                .andExpect(jsonPath("$.reason").value("I'm sick"))
                .andExpect(jsonPath("$.status").value("Pending"));
    }

    // 测试查看请假申请状态
    @Test
    public void testGetLeaveRequestStatus() throws Exception {
        // 首先提交一个请假申请以获取ID
        mockMvc.perform(post("/api/leave/submit")
                        .contentType(MediaType.APPLICATION_JSON)
                        .content(asJsonString(new LeaveRequestDto(1L, "I'm sick"))))
                .andExpect(status().isCreated());

        // 然后获取其状态
        mockMvc.perform(get("/api/leave/status/1")) // 假设ID为1，实际中需要动态获取
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.id").exists())
                .andExpect(jsonPath("$.studentId").value(1L))
                .andExpect(jsonPath("$.reason").value("I'm sick"))
                .andExpect(jsonPath("$.status").value("Pending"));
    }

    // 辅助方法，将对象转换为JSON字符串
    private static String asJsonString(final Object obj) {
        try {
            return new ObjectMapper().writeValueAsString(obj);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
